﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TracNghiemTinHocDaiCuong.ThucThe;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.BIZ;
 

namespace TracNghiemTinHocDaiCuong
{
    public partial class SuaDeThi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DeThi deThi = TimDeThi();
            if (!IsPostBack)
            {
                ddlChuDe.DataSource = ChuDeBIZ.LayDanhSach();
                ddlChuDe.DataBind();
                txtTenDeThi.Text = deThi.TenDT;
                int maChuDe = deThi.MaCD;
                ChuDe cd = ChuDeBIZ.TimTheoId(maChuDe);
                ddlChuDe.SelectedValue = maChuDe.ToString();
                ddlDoKho.SelectedValue = deThi.DoKho.ToString();
                txtSoCauHoi.Text = deThi.SoCauHoi.ToString();
                txtThoiGian.Text = deThi.ThoiGian.ToString();
                txtNgayTao.Text = deThi.NgayTao.ToString();
                if (deThi.DaThi == true)
                    chkDaThi.Checked = true;
                else
                    chkDaThi.Checked = false;
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            DeThi DeThi = new DeThi();
            DeThi.MaDT = (int)ViewState["idDeThi"];
            DeThi.TenDT = txtTenDeThi.Text;
            DeThi.MaCD = Methods.ConvertNumber(ddlChuDe.SelectedValue);
            string doKho = ddlDoKho.SelectedItem.ToString();
            if (doKho == "Dễ")
                DeThi.DoKho = "D";
            if (doKho == "Trung Bình")
                DeThi.DoKho = "T";
            if (doKho == "Khó")
                DeThi.DoKho = "K";
            DeThi.SoCauHoi = Methods.ConvertNumber(txtSoCauHoi.Text);
            DeThi.ThoiGian = Methods.ConvertNumber(txtThoiGian.Text);
            DeThi.NgayTao = Methods.ConvertDateTime(txtNgayTao.Text);
            if (chkDaThi.Checked)
                DeThi.DaThi = true;
            else
                DeThi.DaThi = false;
            DeThiBIZ.Sua(DeThi);
            lblThongBao.Text = "Bạn đã sửa \"" + txtTenDeThi.Text + "\" thành công";
        }

        protected void btnXoaTuKhoa_Click(object sender, EventArgs e)
        {
            txtTenDeThi.Text = "";
        }

        protected void btnQuayLai_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuanLyDeThi.aspx");
        }

        protected void btnSinhDe_Click(object sender, EventArgs e)
        {
            DeThi de = TimDeThi();
            DeThi_CauHoiBIZ.Xoa(de.MaDT);
            int idDeThi = (int)ViewState["idDeThi"];
            DeThi deThi = DeThiBIZ.TimTheoId(idDeThi);
            DeThiBIZ.TaoDeThi(deThi);
        }

        private DeThi TimDeThi()
        {
            ViewState["idDeThi"] = Methods.ConvertNumber(Request.QueryString["ID"]);
            DeThi DeThi = DeThiBIZ.TimTheoId((int)ViewState["idDeThi"]);
            return DeThi;
        }
    }
}